import 'package:flutter/material.dart';

class PoetItem extends StatefulWidget {
  final VoidCallback onAuthorTap;
  final checkLineCallback;

  var shareMode = false;

  PoetItem({this.onAuthorTap, this.shareMode: false, this.checkLineCallback});

  @override
  State<StatefulWidget> createState() {
    return _PoetItem();
  }
}

class _PoetItem extends State<PoetItem> {
  final String _content = '''
剑外忽，
剑外忽xxx，
剑外忽传收蓟北，
初闻涕泪满衣裳。
却看妻子愁何在，
漫卷诗书喜欲狂。
白日放歌须纵酒，
青春作伴好还乡。
即从巴峡穿巫峡，
便下襄阳向洛阳。
''';

  List<bool> _checkList = null;
  List<String> _lines;

  @override
  void initState() {
    super.initState();
    _lines = _content.split(new RegExp("\n")).toList();
    _checkList = List(_lines.length);
    _checkList.fillRange(0, _lines.length, false);
  }

  void _checkLineChange(int index, bool val) {
    setState(() {
      _checkList[index] = val;
    });
    if (widget.checkLineCallback != null) {
      String content = _lines
          .asMap()
          .keys
          .where((i) => _checkList[i])
          .map((i) => _lines[i])
          .join("\r\n");
      widget.checkLineCallback(content);
    }
  }

  Widget _buildContentWidgets(String content) {
    var lines = content.split(new RegExp("\n"));
    List<Widget> texts = lines
        .asMap()
        .keys
        .map((i) => Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  lines[i],
                ),
                widget.shareMode && !lines[i].trim().isEmpty
                    ? Checkbox(
                        value: _checkList[i],
                        onChanged: (v) => _checkLineChange(i, v),
                      )
                    : Text("")
              ],
            ))
        .toList();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: texts,
    );
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints viewportConstraints) {
        return SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minWidth: viewportConstraints.maxWidth,
              minHeight: viewportConstraints.maxHeight,
            ),
            child: Padding(
              padding: EdgeInsets.only(top: 40),
              child: DefaultTextStyle(
                textAlign: TextAlign.center,
                style: Theme.of(context).textTheme.body1.copyWith(
                      fontFamily: 'hyqts',
                      fontSize: 24,
                      height: 1.5,
                    ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      "闻官军收河南河北",
                    ),
                    GestureDetector(
                      onTap: () {
                        (widget.onAuthorTap ?? () => {})();
                      },
                      child: Text(
                        "杜甫",
                        textScaleFactor: 0.8,
                        style: TextStyle(),
                      ),
                    ),
                    _buildContentWidgets(_content),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
